/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClientGui.java
 *
 * Created on Nov 29, 2009, 1:41:04 PM
 */

package sahd.broker.client;

import sahd.broker.proxys.ProxyClient;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import sahd.broker.iONetMessageManager.Message;
import sahd.gui.configuration.ConfigurationUI;
import sahd.gui.manager.ManagerMainUI;
import sahd.nicTools.NICTool;

/**
 *
 * @author Romms
 */
public class ClientGui extends javax.swing.JFrame implements Observer{

    private ProxyClient proxyClient;
    private int outPort=8888;
    private int entryProt=7777;
    private int serviceResponsePort=6666;

    private String initialServerIp="192.168.1.67";
    private Message message;

    /** Creates new form ClientGui */
    public ClientGui() throws IOException {
        initComponents();

        //nuevo codigo


        //viejo codigo
        /**proxyClient = ProxyClient.getInstance(this, outPort,entryProt,serviceResponsePort,
                initialServerIp);**/
        //this.registerClient();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        lblStatus = new javax.swing.JLabel();
        appDesktop = new javax.swing.JDesktopPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        mnuAdministrador = new javax.swing.JMenu();
        mnuConfigurar = new javax.swing.JMenu();
        mnuConexion = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Service Status:");

        lblStatus.setText("Status...");

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        mnuAdministrador.setText("Administrador");
        mnuAdministrador.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuAdministradorActionPerformed(evt);
            }
        });
        jMenuBar1.add(mnuAdministrador);

        mnuConfigurar.setText("Configurar");
        mnuConfigurar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuConfigurarActionPerformed(evt);
            }
        });

        mnuConexion.setText("Conexión");
        mnuConexion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuConexionActionPerformed(evt);
            }
        });
        mnuConfigurar.add(mnuConexion);

        jMenuBar1.add(mnuConfigurar);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblStatus)
                .addContainerGap(278, Short.MAX_VALUE))
            .addComponent(appDesktop, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(appDesktop, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblStatus)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnuAdministradorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuAdministradorActionPerformed
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ManagerMainUI().setVisible(true);
            }
        });
    }//GEN-LAST:event_mnuAdministradorActionPerformed

    private void mnuConexionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuConexionActionPerformed
        ConfigurationUI configurationUI = new ConfigurationUI();
        this.appDesktop.add(configurationUI);
        configurationUI.setSize(420, 400);
        configurationUI.setVisible(true);
    }//GEN-LAST:event_mnuConexionActionPerformed

    private void mnuConfigurarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuConfigurarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_mnuConfigurarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new ClientGui().setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(ClientGui.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }


    private void eventHandler(Message message){
        this.message= message;
        switch(this.message.getProtocol()){
            case ClientProtocol.SUCCESFUL_REGISTER:
                this.lblStatus.setText("Cliente registrado.");
                break;

            default:System.out.println("Invalid protocol");
        }

    }

    public void update(Observable o, Object arg) {
        eventHandler((Message) arg);
    }

    //Services
    private void registerClient(){
        try {
            ClientMachine clientMachine= new ClientMachine(NICTool.getNICIPAddress(), NICTool.getNICMacAddress());
            this.proxyClient.shipMessage(clientMachine, ClientProtocol.REGISTER_CLIENT);
        } catch (IOException ex) {
            Logger.getLogger(ClientGui.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDesktopPane appDesktop;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JMenu mnuAdministrador;
    private javax.swing.JMenuItem mnuConexion;
    private javax.swing.JMenu mnuConfigurar;
    // End of variables declaration//GEN-END:variables

}
